package com.wsd.tank;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * 	坦克窗口
 * @author fengmoboy
 */
public class TankFrame extends Frame {
	
	private static final long serialVersionUID = 1L;
	public static final int GAME_WDITH = PorpertyMgr.build().get("gameWidth");
	public static final int GAME_HEIGHT = PorpertyMgr.build().get("gameHeight");
	List<Bullet> bullets = new ArrayList<>();
	List<Tank> tanks = new ArrayList<>();
	Tank myTank = new Tank(200, 400, Direction.UP, Group.GOOD, this);
	List<Explode> explodes = new ArrayList<>();
	/**
	 * 	构造方法
	 */
	public TankFrame() {
		// 设置大小
		this.setSize(GAME_WDITH, GAME_HEIGHT);
		// 设置可改变大小标识
		this.setResizable(false);
		// 设置标题
		this.setTitle("tank war");
		// 设置可见
		this.setVisible(true);
		
		//添加键盘监听器
		this.addKeyListener(new MyKeyListener());
		
		//添加窗口监听器 实现匿名内部类 监听窗口关闭事件
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
	}
	Image offScreenImage = null;
	// 绘制一个与窗口大小一样的图片 然后在此上绘制坦克游戏相关 最后在将此图片绘制到窗口中
	// 利用双缓冲解决闪烁问题
	@Override
	public void update(Graphics g) {
		if(offScreenImage == null) {
			offScreenImage = this.createImage(GAME_WDITH, GAME_HEIGHT);
		}
		Graphics gOffScreen = offScreenImage.getGraphics();
		Color c = gOffScreen.getColor();
		gOffScreen.setColor(Color.BLACK);
		gOffScreen.fillRect(0, 0, GAME_WDITH, GAME_HEIGHT);
		gOffScreen.setColor(c);
		paint(gOffScreen);
		g.drawImage(offScreenImage, 0, 0, null);
	}
	
	// 绘制坦克游戏相关
	@Override
	public void paint(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.WHITE);
		g.drawString("子弹的数量：" + bullets.size(), 10, 60);
		g.drawString("敌人的数量：" + tanks.size(), 10, 100);
		g.drawString("爆炸的数量：" + explodes.size(), 10, 140);
		g.setColor(c);
		myTank.paint(g);
		for(int i = 0; i < bullets.size(); i++) {
			bullets.get(i).paint(g);	
		}
		
		for(int i = 0; i < tanks.size(); i++) {
			tanks.get(i).paint(g);	
		}
		
		for(int i = 0; i < bullets.size(); i++) {
			for(int j = 0; j < tanks.size(); j++) {
				bullets.get(i).collideWith(tanks.get(j));
			}
		}
		
		for(int i = 0; i < explodes.size(); i++) {
			explodes.get(i).paint(g);
		}
		
	}
	
	/**
	 * 	自定义处理按键相关操作及监听
	 * @author fengmoboy
	 *
	 */
	class MyKeyListener extends KeyAdapter {
		
		/**
		 * 	左方向-标识位
		 */
		boolean bL = false;
		/**
		 * 	上方向-标识位
		 */
		boolean bU = false;
		/**
		 * 	右方向-标识位
		 */
		boolean bR = false;
		/**
		 * 	下方向-标识位
		 */
		boolean bD = false;

		/**
		 *	响应-按键被按下
		 */
		@Override
		public void keyPressed(KeyEvent e) {
			//System.out.println("key Pressed");
			//x += 20;
			// 重绘方法
			//repaint();
			int key = e.getKeyCode();
			switch (key) {
				case KeyEvent.VK_LEFT:
					bL = true;
					break;
				case KeyEvent.VK_UP:
					bU = true;
					break;
				case KeyEvent.VK_RIGHT:
					bR = true;
					break;
				case KeyEvent.VK_DOWN:
					bD = true;
					break;
				default:
					break;
			}
			
			setMainTankDir(myTank);
			
		}
		
		/**
		 *	响应-按键被抬起
		 */
		@Override
		public void keyReleased(KeyEvent e) {
//			try {
//				Thread.sleep(80);
//			} catch (InterruptedException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
			int key = e.getKeyCode();
			switch (key) {
				case KeyEvent.VK_LEFT:
					bL = false;
					break;
				case KeyEvent.VK_UP:
					bU = false;
					break;
				case KeyEvent.VK_RIGHT:
					bR = false;
					break;
				case KeyEvent.VK_DOWN:
					bD = false;
					break;
				case KeyEvent.VK_CONTROL:
					myTank.fire();
					break;
				default:
					break;
			}
			setMainTankDir(myTank);
		}

		/**
		 * 	设置坦克的方向
		 * @param tank
		 */
		private void setMainTankDir(Tank tank) {
			System.out.println("bL: "+ bL + "bU: "+ bU + "bR: "+ bR + "bD: "+ bD );
			if(!bL && !bU && !bR && !bD) {
				tank.setMoving(false);
			} else {
				tank.setMoving(true);
				// 计算方向
				if(bL) {
					tank.setDirection(Direction.LEFT);
				}
				if(bU) {
					tank.setDirection(Direction.UP);
				}
				if(bR) {
					tank.setDirection(Direction.RIGHT);
				}
				if(bD) {
					tank.setDirection(Direction.DOWN);
				}
			}
		}
		
	}
}
